<?php

/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 */

$connection = mysql_connect(constant("DATABASE_HOST") . ':' . constant("DATABASE_PORT"), constant("DATABASE_USER"), constant("DATABASE_PASSWORD"));
$dbname = constant("DATABASE_NAME");

class Db {

    public $connection;
    public $dbname;

    private function DB_STATUS() {

        global $connection;
        global $dbname;

        if (!$connection) {
            #die('Could not connect: ' . mysql_error());
            return false;
        } else {
            $db_selected = mysql_select_db($dbname, $connection);
            if (!$db_selected) {
                return false;
            } else {
                return true;
            } #if db_selected
        } #if connection
    }

    public function DB_CLOSE() {
        global $connection;
        mysql_close($connection);
    }

    public function DB_SELECT($sql) {
        global $connection;
        $mystatus = $this->DB_STATUS();
        if ($mystatus == true) {
            $result = mysql_query($sql, $connection);
            if ($result) {
                return $result;
            }
        }
        return 0;
    }

    public function DB_DELETE($sql) {
        // Returns the deleted objects ID if OK, else 0
        global $connection;
        $mystatus = $this->DB_STATUS();
        if ($mystatus == true) {
            $result = mysql_query($sql, $connection);
        }
        return 0;
    }

    public function DB_UPDATE($sql) {
        // Returns the updated objects ID if OK, else 0
        global $connection;
        $mystatus = $this->DB_STATUS();
        if ($mystatus == true) {
            $result = mysql_query($sql, $connection);
        }
        return 0;
    }

    public function DB_INSERT($sql) {
        // Returns the created objects ID if OK, else 0
        #echo $sql;
        global $connection;
        $mystatus = $this->DB_STATUS();
        if ($mystatus == true) {
            $result = mysql_query($sql, $connection);
        }
        return 0;
    }

    public function DB_COUNT($sql) {
        // Returns the created objects ID if OK, else 0
        global $connection;
        $mystatus = $this->DB_STATUS();
        if ($mystatus == true) {
            $result = mysql_query($sql, $connection);
        }
        return 0;
    }

    /**/

    public function setDbName($dbname) {
        $this->dbname = $dbname;
    }

    public function getDbName() {
        return $this->dbname;
    }

    public function setDbConnection($connection) {
        $this->connection = $connection;
    }

    public function getDbConnection() {
        return $this->connection;
    }

// End class
}
?>